<?php


namespace app\sports\intelligence\service;

use think\Exception;
use app\common\HTTP;
use think\facade\Log;
use app\sports\intelligence\model\IntelligenceModel;
use app\common\InstanceTrait;
class IntelligenceCaiji
{
    private $user = '';
    private  $secret ='';
//    接口名:情报分析 赛前48小时请求
//    接口频次:2/s
    private $url = 'open.sportnanoapi.com/api/sports/football/match/intelligence';

    protected function __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }


    /*
     * 足球情报采集
     *$id 纳米比赛的id
     */
    public function Execs($id=2214059)
    {
       try{
           $httpressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret&id=$id"), true);

           $modeldata = IntelligenceModel::getInstance()->QueryId($id);
           if (!$modeldata){
               $data=[
                'nami_match_id'  =>$id,
                'info'           =>json_encode($httpressult['info'],JSON_UNESCAPED_UNICODE),
                'cahnce'         =>json_encode($httpressult['cahnce'],JSON_UNESCAPED_UNICODE),
                'create_time'    =>date('Y-m-d H:i:s',time()),
                'update_time'    =>date('Y-m-d H:i:s',time()),
            ];
                  IntelligenceModel::getInstance()->InsertIntelligence($data);
           }else{
                $data  = [
                    'info'   => json_encode($httpressult['info'],JSON_UNESCAPED_UNICODE),
                    'cahnce' => json_encode($httpressult['cahnce'],JSON_UNESCAPED_UNICODE),
                    'update_time' =>date('Y-m-d H:i:s',time()),
                ];
                 IntelligenceModel::getInstance()->UpdateIntelligence($id,$data);
           }

         Log::log('info','足球情报采集成功');
       }catch(Exception $e){
           Log::error('足球情报错误:'.$e->getMessage());
            return [];
       }
    }


}